package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.frame.view.linearlistview.LinearListView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.AnalysisRadioAdapter;
import com.kupurui.asteacher.bean.JobAnalysisInfo;
import com.kupurui.asteacher.ui.BaseFgt;
import com.kupurui.asteacher.ui.ShowBigImageAty;
import com.kupurui.asteacher.utils.URLImageParser;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作业解析单选
 * Created by Administrator on 2017/7/26.
 */

public class AnalysisRadioFgt extends BaseFgt {

    @Bind(R.id.tv_but)
    TextView tvBut;
    @Bind(R.id.tv_next)
    TextView tvNext;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.listview)
    LinearListView listview;
    @Bind(R.id.tv_answer)
    TextView tvAnswer;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.tv_size)
    TextView tvSize;
    @Bind(R.id.ll_radio_answer)
    LinearLayout llRadioAnswer;
    @Bind(R.id.tv_correct)
    TextView tvCorrect;
    @Bind(R.id.tv_student_answer)
    TextView tvStudentAnswer;
    @Bind(R.id.ll_question)
    LinearLayout llQuestion;
    @Bind(R.id.iv_student_answer)
    SimpleDraweeView ivStudentAnswer;
    @Bind(R.id.tv_serial_number)
    TextView tvSerialNumber;

    JobAnalysisInfo jobAnalysisInfo;
    private int position = 0;
    private int size = 0;
    private String total = "";
    JobAnalysisAty activity;

    List<String> uris;
    List<JobAnalysisInfo.OptionBean>  list;
    AnalysisRadioAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.analysis_radio_fgt;
    }

    @Override
    public void initData() {
        activity = (JobAnalysisAty) getActivity();
        uris = new ArrayList<>();
        list = new ArrayList<>();
        if (getArguments() != null) {
            jobAnalysisInfo = (JobAnalysisInfo) getArguments().getSerializable("bean");
            position = getArguments().getInt("position");
            size = getArguments().getInt("size");
            total = getArguments().getString("total");
        }
        if (1 == size) {
            tvNext.setVisibility(View.GONE);
        } else {
            tvNext.setVisibility(View.VISIBLE);
        }
        tvSerialNumber.setText("("+jobAnalysisInfo.getQuestion().getSerial_number()+")");
        tvNum.setText(position+"");
        tvSize.setText("/" + total);
        uris.add(jobAnalysisInfo.getUser_answer());
        list.addAll(jobAnalysisInfo.getOption());
        adapter = new AnalysisRadioAdapter(getActivity(),list,R.layout.radio_item);


        tvTitle.setText(Html.fromHtml(jobAnalysisInfo.getQuestion().getTitle(), new URLImageParser(getActivity(),tvTitle), null));
        tvAnswer.setText(Html.fromHtml(jobAnalysisInfo.getQuestion().getAnalysis(), new URLImageParser(getActivity(),tvAnswer), null));
        if (jobAnalysisInfo.getQuestion().getType().equals("1")) {
            tvBut.setText("单选题");
            llRadioAnswer.setVisibility(View.VISIBLE);
            tvCorrect.setText(jobAnalysisInfo.getTrue_answer());
            if (TextUtils.isEmpty(jobAnalysisInfo.getUser_answer())){
                tvStudentAnswer.setText("无");
            }else {
                tvStudentAnswer.setText(jobAnalysisInfo.getUser_answer());
            }
            llQuestion.setVisibility(View.GONE);
            ivStudentAnswer.setVisibility(View.GONE);
            listview.setAdapter(adapter);
        } else if (jobAnalysisInfo.getQuestion().getType().equals("2")) {
            tvBut.setText("判断题");
            llRadioAnswer.setVisibility(View.VISIBLE);
            tvCorrect.setText(jobAnalysisInfo.getTrue_answer());
            if (TextUtils.isEmpty(jobAnalysisInfo.getUser_answer())){
                tvStudentAnswer.setText("无");
            }else {
                tvStudentAnswer.setText(jobAnalysisInfo.getUser_answer());
            }
            llQuestion.setVisibility(View.GONE);
            ivStudentAnswer.setVisibility(View.GONE);
            listview.setAdapter(adapter);
        } else if (jobAnalysisInfo.getQuestion().getType().equals("3")) {
            tvBut.setText("填空题");
            llRadioAnswer.setVisibility(View.GONE);
            llQuestion.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(jobAnalysisInfo.getUser_answer())){
                ivStudentAnswer.setVisibility(View.GONE);
            }else{
                llQuestion.setVisibility(View.VISIBLE);
                ivStudentAnswer.setImageURI(jobAnalysisInfo.getUser_answer());
            }
        } else if (jobAnalysisInfo.getQuestion().getType().equals("4")) {
            tvBut.setText("解答题");
            llRadioAnswer.setVisibility(View.GONE);
            if (TextUtils.isEmpty(jobAnalysisInfo.getUser_answer())){
                ivStudentAnswer.setVisibility(View.GONE);
            }else{
                llQuestion.setVisibility(View.VISIBLE);
                ivStudentAnswer.setImageURI(jobAnalysisInfo.getUser_answer());
            }
        }
    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.tv_next,R.id.iv_student_answer})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_next:
                activity.changeView(position);
                break;
            case R.id.iv_student_answer:
                Bundle bundle1 = new Bundle();
                bundle1.putStringArrayList("pics",new ArrayList<>(uris));
                startActivity(ShowBigImageAty.class,bundle1);
                break;
        }
    }
}
